package com.hzhjhh.view.manageView.menus.query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Vector;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson2.JSON;
import com.hzhjhh.dto.MerchandiseDTO;
import com.hzhjhh.entity.Merchandise;
import com.hzhjhh.enums.CategoryEnums;
import com.hzhjhh.init.dialog.Mytable;
import com.hzhjhh.mapper.MerchandiseMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.util.FieldMapping;
import com.hzhjhh.view.manageView.MainFrame;
import com.hzhjhh.view.manageView.menus.query.constant.ShowConstants;
import com.hzhjhh.view.manageView.menus.query.enums.QueryGoodsShowEnum;

/**
 * 菜单栏初始化-查询-查询商品信息
 * 
 * @author Administrator
 *
 */
public class QueryMerchandiseInfo {

	private static MerchandiseMapper merchandiseMapper;
	
	public static JMenu init(MainFrame mainFrame) {
		JMenu queryMerchandiseInfo = new JMenu();
		queryMerchandiseInfo.setText("查询商品信息");
		
		// 初始化mapper
		merchandiseMapper = DBUtils.getSqlSession(true).getMapper(MerchandiseMapper.class);
		
		// 按商品名称查询
		JMenuItem merchandiseName = new JMenuItem();
		merchandiseName.setText("按商品名称查询");
		merchandiseName.addActionListener(event -> {
			queryByDTO(mainFrame, QueryGoodsShowEnum.NAME);
		});
		
		// 按商品条形码查询
		JMenuItem merchandiseTreatyCode = new JMenuItem();
		merchandiseTreatyCode.setText("按商品条形码查询");
		merchandiseTreatyCode.addActionListener(event -> {
			queryByDTO(mainFrame, QueryGoodsShowEnum.TREATY_CODE);
		});
		
		// 按商品编号查询
		JMenuItem merchandiseCode = new JMenuItem();
		merchandiseCode.setText("按商品编号查询");
		merchandiseCode.addActionListener(event -> {
			queryByDTO(mainFrame, QueryGoodsShowEnum.TREATY_CODE);
		});
		
		// 按商品类别查询
		JMenu merchandiseType = new JMenu();
		merchandiseType.setText("按商品类别查询");
		merchandiseTypeInit(mainFrame, merchandiseType);
		
		queryMerchandiseInfo.add(merchandiseName);
		queryMerchandiseInfo.add(merchandiseTreatyCode);
		queryMerchandiseInfo.add(merchandiseCode);
		queryMerchandiseInfo.add(merchandiseType);
		return queryMerchandiseInfo;
	}
	
	private static void merchandiseTypeInit(MainFrame mainFrame, JMenu merchandiseType) {
		// 电器类查询
		JMenuItem dqQuery = new JMenuItem();
		dqQuery.setText("电器类查询");
		dqQuery.addActionListener(event -> {
			queryByCategory(mainFrame, CategoryEnums.DQ1004);
		});
		
		// 礼品类查询
		JMenuItem lpQuery = new JMenuItem();
		lpQuery.setText("礼品类查询");
		lpQuery.addActionListener(event -> {
			queryByCategory(mainFrame, CategoryEnums.LP1006);
		});
		
		// 日用类查询
		JMenuItem ryQuery = new JMenuItem();
		ryQuery.setText("日用类查询");
		ryQuery.addActionListener(event -> {
			queryByCategory(mainFrame, CategoryEnums.RY1005);
		});
		
		// 蔬果类查询
		JMenuItem sgQuery = new JMenuItem();
		sgQuery.setText("蔬果类查询");
		sgQuery.addActionListener(event -> {
			queryByCategory(mainFrame, CategoryEnums.SG1003);
		});
		
		// 食品类查询
		JMenuItem spQuery = new JMenuItem();
		spQuery.setText("食品类查询");
		spQuery.addActionListener(event -> {
			queryByCategory(mainFrame, CategoryEnums.SP1002);
		});
		
		// 生鲜类查询
		JMenuItem sxQuery = new JMenuItem();
		sxQuery.setText("生鲜类查询");
		sxQuery.addActionListener(event -> {
			queryByCategory(mainFrame, CategoryEnums.SX1001);
		});
		
		merchandiseType.add(dqQuery);
		merchandiseType.add(lpQuery);
		merchandiseType.add(ryQuery);
		merchandiseType.add(sgQuery);
		merchandiseType.add(spQuery);
		merchandiseType.add(sxQuery);
	}
	
	/** 
	 * 根据类别查询
	 * 
	 * @param mainFrame 展示弹出框消息的主体框架
	 * @param categoryEnums 类别枚举
	 */
	public static void queryByCategory(MainFrame mainFrame, CategoryEnums categoryEnums) {
		MerchandiseDTO merchandiseDTO = new MerchandiseDTO();
		merchandiseDTO.setMerchandiseSortId(categoryEnums.getCode());
		List<Merchandise> merchandiseList = merchandiseMapper.selectListByDTO(merchandiseDTO);
		if (Objects.isNull(merchandiseList)) {
			JOptionPane.showMessageDialog(mainFrame, "商品不存在", "提示", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		Vector<Vector<Object>> dataRows = FieldMapping.list2Show(merchandiseList, QueryGoodsShowEnum.descMap(), ShowConstants.QUERY_GOODS_HEAD_NAME);
		JTable jTable1 = mainFrame.getjTable1();
		jTable1 = Mytable.maketable(dataRows, ShowConstants.QUERY_GOODS_HEAD_NAME); // 显示内容
		mainFrame.getjScrollPane2().getViewport().add(jTable1); // 把表装入容器
	}
	/** 
	 * 根据条件查询
	 * 
	 * @param mainFrame 展示弹出框消息的主体框架
	 * @param showEnum 查询条件
	 */
	public static void queryByDTO(MainFrame mainFrame, QueryGoodsShowEnum showEnum) {

		String nameInput = JOptionPane.showInputDialog(mainFrame, "请输入" + showEnum.getColumnName(), "提示", JOptionPane.INFORMATION_MESSAGE);
		if (StringUtils.isBlank(nameInput)) {
			JOptionPane.showMessageDialog(mainFrame, "输入不能为空值", "提示", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put(showEnum.getColumnCode(), nameInput);
		MerchandiseDTO merchandiseDTO = JSON.parseObject(JSON.toJSONString(queryMap), MerchandiseDTO.class);
		Merchandise merchandise = merchandiseMapper.selectOneByDTO(merchandiseDTO);
		if (Objects.isNull(merchandise)) {
			JOptionPane.showMessageDialog(mainFrame, "商品不存在", "提示", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		Vector<String> dataRow = FieldMapping.entity2Show(merchandise, QueryGoodsShowEnum.descMap(), ShowConstants.QUERY_GOODS_HEAD_NAME);
		JTable jTable1 = mainFrame.getjTable1();
		Vector<Vector<String>> rows = new Vector<>();
		rows.add(dataRow);
		jTable1 = Mytable.maketable(rows, ShowConstants.QUERY_GOODS_HEAD_NAME); // 显示内容
		mainFrame.getjScrollPane2().getViewport().add(jTable1); // 把表装入容器
		
	}
}
